package polylegs;
import static polylegs.Type.*;

import java.util.LinkedList;

public class Node {
	
	Type type = EMPTY;
	
	// these data should not be used during the process, they give global coordinates to the nodes
	
	
	int x=-1;
	int y=-1;
	
	Environnement environment = null;
	
	// only those could be used
	// neighbor cells
	public Node topN = null;
	public Node botN = null;
	public Node rightN = null;
	public Node leftN =null;
	
	// gradients
	public int topG = 0;
	public int botG = 0;
	public int rightG = 0;
	public int leftG = 0;
	
	//subgradients
	public int topSubG = 0;
	public int botSubG = 0;
	public int rightSubG = 0;
	public int leftSubG = 0;
	
	//ports
	public boolean topP = false;
	public boolean botP = false;
	public boolean rightP = false;
	public boolean leftP = false;
	
	//type or color of the cell : our mecanism to keep track of the past
	public int color = 0;
	public int new_color =0;
	
	public Node (int x, int y, Environnement env){
		int size_environment = env.config.size_environment;
		this.x=x;
		this.y=y;
		environment= env;
		
		topN = environment.getNode((size_environment+x+1)%size_environment, y);
		botN = environment.getNode((size_environment+x-1)%size_environment, y);
		rightN = environment.getNode(x, (size_environment+y+1)%size_environment);
		leftN = environment.getNode(x, (size_environment+y-1)%size_environment);
		
	}
	
	/*public void isfirstNode(){
		type = NODE;
		rightP= true;
	}*/
	
	public void becomeNode() {
		type = NODE;
		heritsColor();
		
	}
	public void becomeRock(){
		type = ROCK;
	}

	public boolean isRock() {
		return type==ROCK;
	}

	public boolean isEmpty() {
		return type==EMPTY;
	}

	public boolean isNode() {
		return type==NODE;
	}

	// to get an input value (equivalent to gradient)
	public int isRock(int i) {
		if (isRock()){
			return 1;
		}
		return 0;
	}
	
	private void heritsColor(){
		// the color of the new node is chosen randomly  between possible parents
		//(uniform probability over parents)
		// it is an approxiamtion because we do not know the exact parent.
		LinkedList<Integer> list_of_colors_of_parents = new LinkedList<Integer>();
		if(topN.botP == true){
			list_of_colors_of_parents.add(topN.color);
		}
		if(botN.topP == true){
			list_of_colors_of_parents.add(botN.color);
		}
		if(rightN.leftP == true){
			list_of_colors_of_parents.add(rightN.color);
		}
		if(leftN.rightP == true){
			list_of_colors_of_parents.add(leftN.color);
		}
		if(!list_of_colors_of_parents.isEmpty()){
			color = list_of_colors_of_parents.get((int)(Math.random()*list_of_colors_of_parents.size()));
		}
	}
}
